﻿// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Copyright (c) 2011, Dr. Masroor Ehsan
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the author nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

using System;
using System.Collections.Generic;
using System.Linq;

namespace dseScraper
{
    internal class ScheduleChecker
    {
        private readonly List<DayOfWeek> _weekends = new List<DayOfWeek>();
        private readonly List<DateTime> _holidays = new List<DateTime>();

        public ScheduleChecker()
        {
            AddWeekend(DayOfWeek.Friday);
            AddWeekend(DayOfWeek.Saturday);

            AddHoliday(2, 21);
            AddHoliday(3, 26);
            //AddHoliday(4, 15);    // Bangla New Year
            AddHoliday(5, 1);
            AddHoliday(7, 1);       // Bank Holiday
            AddHoliday(8, 15);
            //AddHoliday(12, 7);
            //AddHoliday(12, 14);
            AddHoliday(12, 16);
            AddHoliday(12, 25);
            AddHoliday(12, 31);     // Bank Holiday

            StartTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 11, 01, 0);
            EndTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 15, 03, 0);
        }

        public DateTime StartTime { get; set; }

        public DateTime EndTime { get; set; }

        public void AddWeekend(DayOfWeek value)
        {
            if (!_weekends.Contains(value))
                _weekends.Add(value);
        }

        public void AddHoliday(int month, int day)
        {
            var holiday = new DateTime(1970, month, day);
            _holidays.Add(holiday);
        }

        public DayOfWeek[] GetWeekends()
        {
            return _weekends.ToArray();
        }

        public bool IsWeekend(DayOfWeek dow)
        {
            return _weekends.Contains(dow);
        }

        public void ClearWeekends()
        {
            _weekends.Clear();
        }

        public bool CheckTime(DateTime time)
        {
            if (!CheckDate(time))
                return false;

            if ((time.Hour > StartTime.Hour) || ((time.Hour == StartTime.Hour) && (time.Minute >= StartTime.Minute)))
            {
                if ((time.Hour < EndTime.Hour) || ((time.Hour == EndTime.Hour) && (time.Minute <= EndTime.Minute)))
                    return true;
            }

            return false;
        }

        public bool CheckDate(DateTime date)
        {
            if (GetWeekends().Any(dow => date.DayOfWeek == dow))
            {
                return false;
            }

            return _holidays.All(holiday => (holiday.Day != date.Day) || (holiday.Month != date.Month));
        }
    }
}